package cn.kgc.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.kgc.base.BaseInfoProperties;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.Company;
import cn.kgc.entity.Orders;
import cn.kgc.form.OrdersAddForm;
import cn.kgc.mapper.OrdersMapper;
import cn.kgc.service.CompanyService;
import cn.kgc.service.OrdersService;
import cn.kgc.utils.RedisUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;


/**
 * @author YC
 */
@Slf4j
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Resource
    private CompanyService companyService;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public String insertOrder(OrdersAddForm form) {
        Orders orders = new Orders();
        orders.setUserId(form.getUserId());
        orders.setCompanyId(form.getCompanyId());
        orders.setItemName("");
        orders.setTotalAmount(SystemConstant.TOTAL_AMOUNT);
        orders.setRealPayAmount(SystemConstant.TOTAL_AMOUNT);
        orders.setPostAmount(0);
        orders.setPayMethod(SystemConstant.PAY_METHOD_WEIXIN);
        orders.setStatus(SystemConstant.NO_PAY);
        this.save(orders);
        return orders.getId();
    }

    @Override
    public boolean isVip(String companyId) {
        String key = BaseInfoProperties.REDIS_COMPANY_IS_VIP + ":" + companyId;
        Object data = redisUtil.get(key);
        if (data != null) {
            return (boolean) data;
        }
        //  获取公司信息
        Company company = companyService.getById(companyId);
        if (company == null) {
            return false;
        }
        // 判断是否是会员(is_vip字段的值是1 并且 过期时间大于当前时间)
        boolean flag = Objects.equals(company.getIsVip(), SystemConstant.IS_VIP) && company.getVipExpireDate().after(new Date());
        redisUtil.set(key, flag, 12, TimeUnit.HOURS);
        return flag;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void rechargeSuccess(String orderId) {
        Orders orders = this.getById(orderId);
        orders.setStatus(SystemConstant.SUCCESS_PAY);
        this.updateById(orders);
        // 更新公司信息
        Company company = companyService.getById(orders.getCompanyId());
        company.setIsVip(SystemConstant.IS_VIP);
        // 过期时间
        Date vipExpireDate = company.getVipExpireDate();
        if (vipExpireDate == null || vipExpireDate.before(new Date())) {
            // 本来就不是会员或已经不是会员，会员过期时间从现在开始向后推一个月
            vipExpireDate = DateUtil.offsetMonth(new Date(), 1);
        } else {
            // 还是会员，会员过期时间从上次会员过期时间向后推一个月
            vipExpireDate = DateUtil.offsetMonth(vipExpireDate, 1);
        }
        company.setVipExpireDate(new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000));
    }

}
